//
//  main.c
//  001--数值的转换
//
//  Created by Shawn Li on 2020/5/12.
//  Copyright © 2020 Shawn. All rights reserved.
//

#include <stdio.h>
#include "stdlib.h"
#include "math.h"
#include "time.h"
//题目：将一个十进制数转为 N 进制，输出结果。

#define SUCCESS 1
#define ERROR 0

#define TRUE 1
#define FALSE 0

//存储空间初始分配量
#define MAXSIZE 20

//需要转换到的进制
#define SYSTEMNUM 8


typedef int Status;

typedef int ItemType;


typedef struct {
    ItemType data[MAXSIZE];
//    栈顶
    int top;
} ListStack;

//构建空栈
Status stackInit(ListStack *s){
    s->top = -1;
    return SUCCESS;
}

//清空堆栈
Status stackClear(ListStack *s){
    s->top = -1;
    return SUCCESS;
}

//判空
Status isStackEmpty(ListStack s){
    return s.top == -1 ? TRUE : FALSE;
}

//获得堆栈的长度
int stackLength(ListStack s){
    return s.top + 1;
}

//获取栈顶元素
Status stackGetTop(ListStack s, ItemType *e){
    if (isStackEmpty(s)) {
        return ERROR;
    }
    *e = s.data[s.top];
    return SUCCESS;
}

//压栈
Status stackPush(ListStack *s, ItemType e){
    if (s->top == MAXSIZE - 1) {
        return ERROR;
    }
    
    s->top++;
    s->data[s->top] = e;
    
    return SUCCESS;
}

//弹栈
Status stackPop(ListStack *s, ItemType *e){
    if (isStackEmpty(*s)) {
        return ERROR;
    }
    
    *e = s->data[s->top];
    s->top--;
    
    return SUCCESS;
}


/// 输出一下栈
/// @param s 堆栈
Status stackPrint(ListStack s){
    for (int i = 0; i <= s.top; i++) {
        printf("%d ", s.data[i]);
    }
    
    return SUCCESS;
}



void conversion(int num){
    ListStack s;
    ItemType e;
    
    int num_temp = num;
    
    stackInit(&s);
    
//    将辗转相除的余数入栈
    while (num_temp) {
        stackPush(&s, num_temp % SYSTEMNUM);
        num_temp = num_temp / SYSTEMNUM;
    }
    
    
    printf("十进制下 %d 转为 %d 进制编码为：", num, SYSTEMNUM);
    while (!isStackEmpty(s)) {
        stackPop(&s, &e);
        printf("%d", e);
    }
    
    printf(" .\n");
    
}



int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    
    conversion(128);
    return 0;
}
